package com.shop2cn.iapi.sqrefund.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.sqrefund.parameter.SaveOrUpdateRefundAddressReq;
import com.shop2cn.iapi.sqrefund.service.SaveOrUpdateRefundAddressCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P3;
import com.ymttest.common.order.userquery.util.Utils;
import com.ymttest.database.model.sqrefund.MchRefundAddress;
import com.ymttest.database.sqlwapper.sqrefund.MchRefundAddressWrapper;
import com.ymttest.utils.TestDataManager;

/**
 * 发货 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SaveOrUpdateRefundAddress {

	private static SaveOrUpdateRefundAddressReq saveOrUpdateRefundAddressReq;
	private static SaveOrUpdateRefundAddressCall saveOrUpdateRefundAddressCall;
	
	private static MchRefundAddressWrapper mchRefundAddressWrapper = new MchRefundAddressWrapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("保存或更新退货地址");
	}

	@Before
	public void caseUp() {

		saveOrUpdateRefundAddressReq = new SaveOrUpdateRefundAddressReq();
		saveOrUpdateRefundAddressCall = new SaveOrUpdateRefundAddressCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(saveOrUpdateRefundAddressCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verify(boolean first) {
		
		MchRefundAddress actualResult = null;
		
		boolean isDefaultAddress = saveOrUpdateRefundAddressReq.getDefaultAddress();
		
		if (Utils.zeroIfNull(saveOrUpdateRefundAddressReq.getId()) == 0) {

			actualResult = mchRefundAddressWrapper
					.selectByMerchantId(saveOrUpdateRefundAddressReq
							.getMerchantId());
			
			if (first)
				isDefaultAddress = true;
		} else {
			
			actualResult = mchRefundAddressWrapper
					.selectByPrimaryKey(saveOrUpdateRefundAddressReq.getId());
		}
		
		Logger.verifyEquals(saveOrUpdateRefundAddressReq.getMerchantId(),
				actualResult.getMch_id(), "验证Mch_id");
		Logger.verifyEquals(saveOrUpdateRefundAddressReq
				.getRefundReceiverMobilePhoneAreaCode(), actualResult
				.getRefund_receiver_mobile_phone_area_code(),
				"验证Refund_receiver_mobile_phone_area_code");
		Logger.verifyEquals(
				saveOrUpdateRefundAddressReq.getRefundReceiverMobilePhone(),
				actualResult.getRefund_receiver_mobile_phone(),
				"验证Refund_receiver_mobile_phone");
		Logger.verifyEquals(
				saveOrUpdateRefundAddressReq.getRefundReceiverAddress(),
				actualResult.getRefund_receiver_address(),
				"验证Refund_receiver_address");
		Logger.verifyEquals(saveOrUpdateRefundAddressReq.getRefundReceiver(),
				actualResult.getRefund_receiver(), "验证Refund_receiver");
		Logger.verifyEquals(
				saveOrUpdateRefundAddressReq.getRefundReceiverLandline(),
				actualResult.getRefund_receiver_address(),
				"验证Refund_receiver_address");
		Logger.verifyEquals(false, actualResult.getIs_deleted(), "验证Is_deleted");
		Logger.verifyEquals(isDefaultAddress,
				actualResult.getDefault_address(), "验证Default_address");
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SaveOrUpdateRefundAddress_001() {
		Logger.start(false, "保存退货地址成功");
		try {

//			saveOrUpdateRefundAddressReq.setId(id);
			saveOrUpdateRefundAddressReq.setDefaultAddress(false);
			saveOrUpdateRefundAddressReq.setMerchantId((long) TestDataManager
					.getRandomIntNum(8));
			saveOrUpdateRefundAddressReq.setRefundReceiver("refundReceiver");
			saveOrUpdateRefundAddressReq
					.setRefundReceiverAddress("refundReceiverAddress");
			saveOrUpdateRefundAddressReq
					.setRefundReceiverLandline("refundReceiverLandline");
			saveOrUpdateRefundAddressReq
					.setRefundReceiverMobilePhone("refundReceiverMobilePhone");
			saveOrUpdateRefundAddressReq
					.setRefundReceiverMobilePhoneAreaCode(TestDataManager
							.randomStr(8));
			saveOrUpdateRefundAddressReq.setUserId(235235L);

			saveOrUpdateRefundAddressCall.setData(saveOrUpdateRefundAddressReq);
			saveOrUpdateRefundAddressCall.callService();
			
			this.verify(true);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SaveOrUpdateRefundAddress_002() {
		Logger.start(false, "更新退货地址成功");
		try {

			saveOrUpdateRefundAddressReq.setId(6L);
			saveOrUpdateRefundAddressReq.setDefaultAddress(true);
			saveOrUpdateRefundAddressReq.setMerchantId(92080303L);
			saveOrUpdateRefundAddressReq.setRefundReceiver("refundReceiver");
			saveOrUpdateRefundAddressReq
					.setRefundReceiverAddress("refundReceiverAddress");
			saveOrUpdateRefundAddressReq
					.setRefundReceiverLandline("refundReceiverLandline");
			saveOrUpdateRefundAddressReq
					.setRefundReceiverMobilePhone("refundReceiverMobilePhone");
			saveOrUpdateRefundAddressReq
					.setRefundReceiverMobilePhoneAreaCode(TestDataManager
							.randomStr(8));
			saveOrUpdateRefundAddressReq.setUserId(235235L);

			saveOrUpdateRefundAddressCall.setData(saveOrUpdateRefundAddressReq);
			saveOrUpdateRefundAddressCall.callService();
			
			this.verify(false);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SaveOrUpdateRefundAddress_003() {
		Logger.start(false, "同一个商户已经设置了默认地址，再次设置失败");
		try {
			
			saveOrUpdateRefundAddressReq.setDefaultAddress(true);
			saveOrUpdateRefundAddressReq.setMerchantId(63463455L);
			saveOrUpdateRefundAddressReq.setRefundReceiver("refundReceiver");
			saveOrUpdateRefundAddressReq
					.setRefundReceiverAddress("refundReceiverAddress");
			saveOrUpdateRefundAddressReq
					.setRefundReceiverLandline("refundReceiverLandline");
			saveOrUpdateRefundAddressReq
					.setRefundReceiverMobilePhone("refundReceiverMobilePhone");
			saveOrUpdateRefundAddressReq
					.setRefundReceiverMobilePhoneAreaCode(TestDataManager
							.randomStr(8));
			saveOrUpdateRefundAddressReq.setUserId(235235L);

			saveOrUpdateRefundAddressCall.setData(saveOrUpdateRefundAddressReq);
			saveOrUpdateRefundAddressCall.callService();
			
			saveOrUpdateRefundAddressCall.callService();
			
			Logger.verifyEquals("已存在其他默认地址",
					saveOrUpdateRefundAddressCall.getMsg(), "验证message");
			Logger.verifyEquals(3, saveOrUpdateRefundAddressCall.getCode(),
					"验证message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SaveOrUpdateRefundAddress_004() {
		Logger.start(false, "保存非默认退货地址成功");
		try {

			saveOrUpdateRefundAddressReq.setDefaultAddress(false);
			saveOrUpdateRefundAddressReq.setMerchantId(81119473L);
			saveOrUpdateRefundAddressReq.setRefundReceiver("refundReceiver");
			saveOrUpdateRefundAddressReq
					.setRefundReceiverAddress("refundReceiverAddress");
			saveOrUpdateRefundAddressReq
					.setRefundReceiverLandline("refundReceiverLandline");
			saveOrUpdateRefundAddressReq
					.setRefundReceiverMobilePhone("refundReceiverMobilePhone");
			saveOrUpdateRefundAddressReq
					.setRefundReceiverMobilePhoneAreaCode(TestDataManager
							.randomStr(8));
			saveOrUpdateRefundAddressReq.setUserId(235235L);

			saveOrUpdateRefundAddressCall.setData(saveOrUpdateRefundAddressReq);
			saveOrUpdateRefundAddressCall.callService();
			
			this.verify(false);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
